<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-09-27
 * Time: 13:22
 */

namespace iBrand\Shop\Server\Http\Controllers\O2oUser;

use iBrand\Component\Discount\Services\DiscountService;
use iBrand\Component\Product\Repositories\GoodsRepository;
use iBrand\Server\Http\Controllers\Controller;
use iBrand\Shop\Core\Models\O2oGoodsProducts;
use iBrand\Shop\Server\Service\GoodsService;
use iBrand\Component\Product\Models\Specification;

class GoodsController extends Controller
{

    protected $goodsService;
    protected $goodsRepository;
    protected $discountService;

    public function __construct(
        GoodsService $goodsService,
        GoodsRepository $goodsRepository,
        DiscountService $discountService)

    {
        $this->goodsService = $goodsService;
        $this->goodsRepository = $goodsRepository;
        $this->discountService=$discountService;
    }


    /**
     * 用户门店下单选择sku
     * @param $id
     * @return \Dingo\Api\Http\Response
     */
    public function getStock($id)
    {
        $goods = $this->goodsRepository->findOneById($id);
        $specs = [];
        $stores = [];
        $skuPhoto = collect();

        if ($goods AND count($products = $goods->products()->with('photo')->get())) {
            $grouped = $goods->specificationValue->groupBy('spec_id');

            foreach ($products as $key => $val) {
                $specArray = $val->specID;
                asort($specArray);

                $spec_id = implode('-', $specArray);
                $stores[$spec_id]['id'] = $val->id;

                $o2oProduct = O2oGoodsProducts::where('shop_id', request('shop_id'))->where('product_id', $val->id)->first();
                if (!$o2oProduct) {
                    return $this->api([], false, 500, '该门店暂无商品');
                }

                $stores[$spec_id]['store'] = $o2oProduct->store_nums;

                $singleDiscount = $this->discountService->getSingleDiscountByGoods($goods);
                $stores[$spec_id]['price'] = $this->discountService->getProductPriceFromSingleDiscount($val, $singleDiscount);
                $stores[$spec_id]['sku'] = $val->sku;
                $stores[$spec_id]['ids'] = $val->specID;
                $stores[$spec_id]['redeem_point'] = $goods->redeem_point;

                //产品图片
                if ($photo = $val->photo) {
                    $skuPhotoData['spec_value_id'] = $spec_id;
                    $skuPhotoData['photo'] = $photo->url;
                    $skuPhoto->push($skuPhotoData);
                }
            }

            $i = 1;
            foreach ($grouped as $key => $item) {

                /*$sort = $key == 1 ? $key + 2 : $key;*/
                $keys = $grouped->keys()->toArray();
                if (in_array(2, $keys)) {   //如果有颜色规格，因为颜色ID=2，为了保证 颜色排前面，需要这样处理sort
                    $sort = $key == 1 ? $key + 2 : $key;
                } else {
                    $sort = $key;
                }

                $specs[$sort]['id'] = $key;

                $spec = Specification::find($key);

                if (count($grouped) == 1) {  //如果是单规格
                    if ($key == 2) {  //如果是颜色
                        $specs[$sort]['label_key'] = 'color';
                    } else {  //否则是其他规格
                        $specs[$sort]['label_key'] = 'size';
                    }
                } else {  //如果是双规格
                    if (in_array(2, $keys)) { //如果有颜色规格
                        if ($key == 2) {  //如果是颜色
                            $specs[$sort]['label_key'] = 'color';
                        } else {  //否则是其他规格
                            $specs[$sort]['label_key'] = 'size';
                        }
                    } else {  //没有颜色规格
                        if ($i == 1) {
                            $specs[$sort]['label_key'] = 'color';
                        } else {
                            $specs[$sort]['label_key'] = 'size';
                        }
                    }
                }
                $i++;

                $specs[$sort]['label'] = $spec->name;
                $specs[$sort]['list'] = [];
                $item = $item->sortBy('pivot.sort')->values();
                foreach ($item as $k => $value) {
                    $list = [];
                    $list['id'] = $value->id;
                    $list['value'] = $value->name;

                    if ($value->spec_id == 2)    //颜色
                    {
                        $list['color'] = '#' . $value->rgb;

                        $list['img'] = $this->goodsService->getImageCdnUrl($value->pivot->img);

                        $list['spec_img'] = $this->goodsService->getImageCdnUrl($value->pivot->img);
                    }
                    $list['alias'] = $value->pivot->alias;
                    array_push($specs[$sort]['list'], $list);
                }
            }
        }

        return $this->api([
            'specs' => $specs,
            'stores' => $stores,
        ]);
    }
}
